package java8.concurrent;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.IntStream;

public class Lock1 {
    private static int count = 0;
    private static ReentrantLock lock = new ReentrantLock();


    private static void increment(){
        lock.lock();
        try{
            count++;

        }finally {
            lock.unlock();
        }

    }

    public static void main(String[] args) {
        count = 0;

        //https://blog.csdn.net/bairrfhoinn/article/details/16848785
        //创建一个可以容纳2个线程任务的线程池
        ExecutorService executor = Executors.newFixedThreadPool(2);

        IntStream.range(0, 1000).forEach(i -> executor.submit(Lock1::increment));

        executor.shutdown();

        System.out.println("all:" + count);
    }
}
